package mongodb.homework.week.three.one;

import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class AggregationTest {

	/**
	 * @param args
	 * @throws UnknownHostException
	 */
	public static void main(String[] args) throws UnknownHostException {
		MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
		DB db = mongoClient.getDB("mydb");
		DBCollection collection = db.getCollection("test");

		DBObject unwind = new BasicDBObject("$unwind", "$array");
		DBObject match = new BasicDBObject("$match", new BasicDBObject(
				"array.value", "value2"));
		DBObject project = new BasicDBObject("$project", new BasicDBObject(
				"_id", 0).append("array", 1));

		List<DBObject> pipeline = Arrays.asList(unwind, match, project);
		AggregationOutput output = collection.aggregate(pipeline);

		Iterable<DBObject> results = output.results();

		for (DBObject result : results) {
			System.out.println(result.get("array"));
		}
	}

}
